﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Profiling.Recorder
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using Unity.Profiling;
using Unity.Profiling.LowLevel;
using Unity.Profiling.LowLevel.Unsafe;
using UnityEngine.Scripting;

#nullable disable
namespace UnityEngine.Profiling
{
  /// <summary>
  ///   <para>Records profiling data produced by a specific Sampler.</para>
  /// </summary>
  [UsedByNativeCode]
  public sealed class Recorder
  {
    private const ProfilerRecorderOptions s_RecorderDefaultOptions = (ProfilerRecorderOptions) 153;
    internal static Recorder s_InvalidRecorder = new Recorder();
    private ProfilerRecorder m_RecorderCPU;
    private ProfilerRecorder m_RecorderGPU;

    internal Recorder()
    {
    }

    internal Recorder(ProfilerRecorderHandle handle)
    {
      if (!handle.Valid)
        return;
      this.m_RecorderCPU = new ProfilerRecorder(handle, options: (ProfilerRecorderOptions) 153);
      if ((ProfilerRecorderHandle.GetDescription(handle).Flags & MarkerFlags.SampleGPU) == 0)
        return;
      this.m_RecorderGPU = new ProfilerRecorder(handle, options: (ProfilerRecorderOptions) 217);
    }

    ~Recorder()
    {
      this.m_RecorderCPU.Dispose();
      this.m_RecorderGPU.Dispose();
    }

    /// <summary>
    ///   <para>Use this function to get a Recorder for the specific Profiler label.</para>
    /// </summary>
    /// <param name="samplerName">Sampler name.</param>
    /// <returns>
    ///   <para>Recorder object for the specified Sampler.</para>
    /// </returns>
    public static Recorder Get(string samplerName)
    {
      ProfilerRecorderHandle handle = ProfilerRecorderHandle.Get(ProfilerCategory.Any, samplerName);
      return !handle.Valid ? Recorder.s_InvalidRecorder : new Recorder(handle);
    }

    /// <summary>
    ///   <para>Returns true if Recorder is valid and can collect data. (Read Only)</para>
    /// </summary>
    public bool isValid => this.m_RecorderCPU.handle > 0UL;

    /// <summary>
    ///   <para>Enables recording.</para>
    /// </summary>
    public bool enabled
    {
      get => this.m_RecorderCPU.IsRunning;
      set => this.SetEnabled(value);
    }

    /// <summary>
    ///   <para>Accumulated time of Begin/End pairs for the previous frame in nanoseconds. (Read Only)</para>
    /// </summary>
    public long elapsedNanoseconds => !this.m_RecorderCPU.Valid ? 0L : this.m_RecorderCPU.LastValue;

    /// <summary>
    ///   <para>Gets the accumulated GPU time, in nanoseconds, for a frame. The Recorder has a three frame delay so this gives the timings for the frame that was three frames before the one that you access this property on. (Read Only).</para>
    /// </summary>
    public long gpuElapsedNanoseconds
    {
      get => !this.m_RecorderGPU.Valid ? 0L : this.m_RecorderGPU.LastValue;
    }

    /// <summary>
    ///   <para>Number of time Begin/End pairs was called during the previous frame. (Read Only)</para>
    /// </summary>
    public int sampleBlockCount
    {
      get
      {
        return !this.m_RecorderCPU.Valid || this.m_RecorderCPU.Count != 1 ? 0 : (int) this.m_RecorderCPU.GetSample(0).Count;
      }
    }

    /// <summary>
    ///   <para>Gets the number of Begin/End time pairs that the GPU executed during a frame. The Recorder has a three frame delay so this gives the timings for the frame that was three frames before the one that you access this property on. (Read Only).</para>
    /// </summary>
    public int gpuSampleBlockCount
    {
      get
      {
        return !this.m_RecorderGPU.Valid || this.m_RecorderGPU.Count != 1 ? 0 : (int) this.m_RecorderGPU.GetSample(0).Count;
      }
    }

    /// <summary>
    ///   <para>Configures the recorder to only collect data from the current thread.</para>
    /// </summary>
    public void FilterToCurrentThread()
    {
      if (!this.m_RecorderCPU.Valid)
        return;
      this.m_RecorderCPU.FilterToCurrentThread();
    }

    /// <summary>
    ///   <para>Configures the recorder to collect samples from all threads.</para>
    /// </summary>
    public void CollectFromAllThreads()
    {
      if (!this.m_RecorderCPU.Valid)
        return;
      this.m_RecorderCPU.CollectFromAllThreads();
    }

    private void SetEnabled(bool state)
    {
      if (state)
      {
        this.m_RecorderCPU.Start();
        if (!this.m_RecorderGPU.Valid)
          return;
        this.m_RecorderGPU.Start();
      }
      else
      {
        this.m_RecorderCPU.Stop();
        if (this.m_RecorderGPU.Valid)
          this.m_RecorderGPU.Stop();
      }
    }
  }
}
